#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    struct PairCompare
    {
        bool operator()(pair<string, int>& p1, pair<string, int>& p2)
        {
            return p1.second > p2.second || (p1.second == p2.second && p1.first < p2.first);
        }
    };
    vector<string> topKFrequent(vector<string>& words, int k)
    {
        map<string, int> m;
        for (int i = 0; i < words.size(); i++)
        {
            m[words[i]]++;
        }
        vector<pair<string, int>> v(m.begin(), m.end());
        sort(v.begin(), v.end(), PairCompare());
        vector<string> vv;
        for (int i = 0; i < k; i++)
        {
            vv.push_back(v[i].first);
        }
        return vv;
    }
};